package com.txd.mofa.shouye.aty;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.R;
import com.txd.mofa.adapter.JubaoAdapter;
import com.txd.mofa.adapter.JubaoAdapter1;
import com.txd.mofa.base.BaseAty;
import com.txd.mofa.fabu.aty.PaizhaoAty1;
import com.txd.mofa.net.NetUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Layout(R.layout.aty_jubao)
public class JubaoAty extends BaseAty {

    private ImageView iv_back;
    private RecyclerView recy, recy1;
    private JubaoAdapter jubaoAdapter;
    private JubaoAdapter1 jubaoAdapter1;
    private Button btn;
    private String id;
    private String buff;
    private int pos;
    private List<Map<String, String>> zhi;
    private int page;

    @Override
    public void initViews() {
        super.initViews();

        iv_back = findViewById(R.id.iv_back);
        recy = findViewById(R.id.recy);
        recy1 = findViewById(R.id.recy1);
        btn = findViewById(R.id.btn);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(me, 4);
        recy.setLayoutManager(gridLayoutManager);

        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(me, 4);
        recy1.setLayoutManager(gridLayoutManager1);

        jubaoAdapter = new JubaoAdapter(R.layout.item_jubao);
        recy.setAdapter(jubaoAdapter);

        jubaoAdapter1 = new JubaoAdapter1(R.layout.item_jubao);
        recy1.setAdapter(jubaoAdapter1);


    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    public void initDatas(JumpParameter paramer) {

        id = paramer.getString("id");
        pos = paramer.getInt("pos");
        zhi = (List<Map<String, String>>) paramer.get("zhi");
        page = paramer.getInt("page");
        buff = "1";

        http();

    }

    private String report_title = "";
    private String content = "";

    private void http() {
        HttpRequest.POST(me, report, new Parameter().add("token", token)
                .add("buff", buff)
                .add("report_title", report_title)
                .add("content", content)
                .add("hair_id", id), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    if (map.get("code").equals("1")) {
                        if (buff.equals("1")) {
                            ArrayList<Map<String, String>> data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                            for (int i = 0; i < data.size(); i++) {
                                data.get(i).put("flag", "1");
                            }
                            data.get(0).put("flag", "2");
                            report_title = data.get(0).get("report_title");
                            jubaoAdapter.setNewData(data);

                            ArrayList<Map<String, String>> details = JSONUtils.parseKeyAndValueToMapList(data.get(0).get("details"));
                            for (int i = 0; i < details.size(); i++) {
                                details.get(i).put("flag", "1");
                            }
                            jubaoAdapter1.setNewData(details);
                        }else if (buff.equals("2")) {
                            setResponse(new JumpParameter().put("zhi",zhi).put("pos",pos).put("page",page));
                            Toast.makeText(me, map.get("message"), Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }


                } else {
                    toast("网络异常");
                }
            }
        });
    }

    private String a = "1";
    private String aa = "1";

    @Override
    public void setEvents() {

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(JubaoAty.this)) {
                    if (content.equals("")) {
                        Toast.makeText(me, "请选择原因", Toast.LENGTH_SHORT).show();
                    } else {
                        buff = "2";

                        http();
                    }
                }else {
                    Toast.makeText(JubaoAty.this, "网络连接不可用，请重试", Toast.LENGTH_SHORT).show();
                }
            }
        });

        jubaoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> data = jubaoAdapter.getData();
                for (int i = 0; i < data.size(); i++) {
                    data.get(i).put("flag", "1");
                }
                data.get(position).put("flag", "2");
                jubaoAdapter.notifyDataSetChanged();

                report_title = data.get(position).get("report_title");

                ArrayList<Map<String, String>> details = JSONUtils.parseKeyAndValueToMapList(data.get(position).get("details"));
                for (int i = 0; i < details.size(); i++) {
                    details.get(i).put("flag", "1");
                }
                jubaoAdapter1.setNewData(details);

                btn.setBackground(getResources().getDrawable(R.mipmap.button_hei));


            }
        });

        jubaoAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> data = jubaoAdapter1.getData();
                for (int i = 0; i < data.size(); i++) {
                    data.get(i).put("flag", "1");
                }
                data.get(position).put("flag", "2");
                jubaoAdapter1.notifyDataSetChanged();

                content = data.get(position).get("content");

                btn.setBackground(getResources().getDrawable(R.mipmap.button_huang));
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResponse(new JumpParameter().put("zhi",zhi).put("pos",pos).put("page",page));
                finish();
            }
        });
    }
}
